package com.farm.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.farm.dao.BookDao;
import com.farm.dao.ProduceDao;
import com.farm.domain.*;
import com.farm.service.ProduceService;
import com.farm.service.AccountService;
import com.farm.utils.DateUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@AllArgsConstructor
public class ProduceServiceImpl implements ProduceService {

    AccountService accountService;

    ProduceDao produceDao;
    BookDao bookDao;

    @Override
    public JSONArray getAllProduce(Integer pageNum, Integer pageSize) {
        JSONArray records = new JSONArray();
        JSONArray finalRecords = new JSONArray();
        JSONObject finalRecord = new JSONObject();

        PageHelper.startPage(pageNum,pageSize);
        Page<Produce> allProduce = produceDao.getAllProduce();

        if (allProduce.size()==0){
            return finalRecords;
        }
        for (Produce info:allProduce) {
            JSONObject record = new JSONObject();
            String uuid = info.getProduceID();
            List<ProduceImg> imgs = produceDao.getMerchantProduceImg(uuid);
            String img = "";
            if (imgs.size()==0){
                img = "/img/produceImg/default.png";
            }else{
                img = imgs.get(0).getImg();
            }
            record.put("uuid",info.getProduceID());
            record.put("name",info.getName());
            record.put("img",img);
            record.put("price",info.getPrice());
            record.put("time",info.getCollectionDate());
            record.put("produceType",info.getProduceType());
            records.add(record);
        }

        finalRecord.put("data",records);
        finalRecord.put("total",allProduce.getTotal());
        finalRecord.put("pages",allProduce.getPages());
        finalRecord.put("pageSize",allProduce.getPageSize());
        finalRecord.put("pageNum",allProduce.getPageNum());
        finalRecords.add(finalRecord);
        int a = 1;
        return finalRecords;
    }


    @Override
    public JSONArray getProduceChooseType(Integer pageNum, Integer pageSize, String produceType) {
        JSONArray records = new JSONArray();
        JSONArray finalRecords = new JSONArray();
        JSONObject finalRecord = new JSONObject();
        PageHelper.startPage(pageNum,pageSize);

        Page<Produce> producePages = produceDao.getProduceChooseType(produceType);
        if (producePages.size()==0){
            return finalRecords;
        }
        for (Produce info:producePages) {
            JSONObject record = new JSONObject();
            String uuid = info.getProduceID();
            List<ProduceImg> imgs = produceDao.getMerchantProduceImg(uuid);
            String img = "";
            if (imgs.size()==0){
                img = "/img/produceImg/default.png";
            }else{
                img = imgs.get(0).getImg();
            }
            record.put("uuid",info.getProduceID());
            record.put("name",info.getName());
            record.put("img",img);
            record.put("price",info.getPrice());
            record.put("time",info.getCollectionDate());
            record.put("produceType",info.getProduceType());
            records.add(record);
        }
        finalRecord.put("data",records);
        finalRecord.put("total",producePages.getTotal());
        finalRecord.put("pages",producePages.getPages());
        finalRecord.put("pageSize",producePages.getPageSize());
        finalRecord.put("pageNum",producePages.getPageNum());
        finalRecords.add(finalRecord);

        return finalRecords;
    }

    @Override
    public JSONArray searchProduce(Integer pageNum, Integer pageSize,String searchName) {
        JSONArray records = new JSONArray();
        JSONArray finalRecords = new JSONArray();
        JSONObject finalRecord = new JSONObject();
        PageHelper.startPage(pageNum,pageSize);

        Page<Produce> searchList = produceDao.searchProduce(searchName);
        if (searchList.size()==0){
            return finalRecords;
        }
        for (Produce info:searchList) {
            JSONObject record = new JSONObject();
            String uuid = info.getProduceID();
            List<ProduceImg> imgs = produceDao.getMerchantProduceImg(uuid);
            String img = "";
            if (imgs.size()==0){
                img = "/img/produceImg/default.png";
            }else{
                img = imgs.get(0).getImg();
            }
            record.put("uuid",info.getProduceID());
            record.put("name",info.getName());
            record.put("img",img);
            record.put("price",info.getPrice());
            record.put("time",info.getCollectionDate());
            record.put("produceType",info.getProduceType());
            records.add(record);
        }
        finalRecord.put("data",records);
        finalRecord.put("total",searchList.getTotal());
        finalRecord.put("pages",searchList.getPages());
        finalRecord.put("pageSize",searchList.getPageSize());
        finalRecord.put("pageNum",searchList.getPageNum());
        finalRecords.add(finalRecord);

        return finalRecords;

    }

    @Override
    public Integer imsertProduceImg(ProduceImg produceImg) {
        return produceDao.imsertProduceImg(produceImg);
    }

    @Override
    public Integer deleteImg(String fileName) {

        return produceDao.deleteImg(fileName);
    }

    @Override
    public Integer addProduce(Produce produce) {
        return produceDao.addProduce(produce);
    }

    @Override
    public List<Produce> getMerchantProduce(String merchantID) {
        return produceDao.getMerchantProduce(merchantID);
    }

    @Override
    public JSONArray getMerchantProduceImg(List<Produce> produceList) {
        JSONArray records = new JSONArray();
        for (Produce produce: produceList) {
            JSONObject record = new JSONObject();
            String produceID=produce.getProduceID();
            List<ProduceImg> imgList = produceDao.getMerchantProduceImg(produceID);
            String img = "";
            if (imgList.size()==0){
                img = "/img/produceImg/default.png";
            }else{
                img = imgList.get(0).getImg();
            }
            record.put("uuid",produce.getProduceID());
            record.put("name",produce.getName());
            record.put("img",img);
            record.put("price",produce.getPrice());
            record.put("count",produce.getCount());
            record.put("time",produce.getCollectionDate());
            record.put("produceType",produce.getProduceType());
            records.add(record);

        }
        return records;
    }

    @Override
    public JSONArray getProduceDetail(String produceID) {

        JSONArray records = new JSONArray();
        JSONObject record = new JSONObject();
        Produce info = produceDao.getProduceInfo(produceID, "");
        Account merchantInfo = accountService.getUserInfoById(info.getMerchantID());
        String merchantName = merchantInfo.getName();
        if (info==null){
            return null;
        }
        List<ProduceImg> imgs = produceDao.getMerchantProduceImg(produceID);
        record.put("data",info);
        record.put("imgList",imgs);
        record.put("merchantName",merchantName);
        records.add(record);

        return records;
    }

    @Override
    public Integer updateProduceDetail(String uuid,String name, String price, String describe,String produceType,String count) {
        Produce produce = new Produce();
        produce.setProduceID(uuid);
        produce.setName(name);
        produce.setPrice(price);
        produce.setDescribe(describe);
        produce.setCount(count);
        produce.setProduceType(produceType);

        return produceDao.updateProduceDetail(produce);

    }

    @Override
    public Integer deleteProduce(String uuid) {
        String path = "G:\\Documents\\GraduationProject\\wisdom-farm-ssm\\src\\main\\resources\\static\\assest\\img\\produceImg";

        List<BookItem> bookItemFlag = bookDao.getBookItemByProduceID(uuid);
        if (bookItemFlag!=null && bookItemFlag.size()>0){
            Integer res = produceDao.updateStatus(uuid);
            if (res!=0){
                log.info("商品在订单信息中存在，更改商品状态为下架！");
                return 1;
            }else {
                return 0;
            }
        }

        Integer result = produceDao.deleteProduce(uuid);
        if (result==1){
            log.info("商品信息删除成功！");
            List<ProduceImg> imgList = produceDao.selectAllImg(uuid);
            for (ProduceImg imgs:imgList) {
                try{
                    String img = imgs.getImg();
                    String fileName = img.substring(img.lastIndexOf("/")+1);
                    File targetFile = new File(path);
                    File deleteFile = new File(targetFile, fileName);
                    boolean delFlag = deleteFile.delete();
                    if (delFlag){
                        log.info("商品资源本地目录下删除成功！");
                        result = produceDao.deleteImg(fileName);
                    }else{
                        log.info("商品资源本地目录下删除失败！");
                        result = produceDao.deleteImg(fileName);
                    }
                    if (result==1){
                        log.info("商品图片信息同步数据库删除成功！");
                    }else{
                        log.info("商品图片信息同步数据库删除失败！");
                    }
                }catch (Exception e) {
                    continue;
                }
            }
            produceDao.delProduceGrow(uuid);

        }else{
            return 0;
        }

        return 1;
    }

    @Override
    public Integer addProduceGrowup(Map params) {

        String uuid = (String) params.get("uuid");
        String growthCycle = (String) params.get("growthCycle");
        String lightDuration = (String) params.get("lightDuration");
        String temperature = (String) params.get("temperature");
        String soilPH = (String) params.get("soilPH");
        String pesticideDose = (String) params.get("pesticideDose");
        String otherDescription = (String) params.get("otherDescription");
        String growUp = (String) params.get("growUp");

        ProduceGrowUp produceGrowUp = new ProduceGrowUp();
        produceGrowUp.setProduceID(uuid);
        produceGrowUp.setGrowthCycle(growthCycle);
        produceGrowUp.setLightDuration(lightDuration);
        produceGrowUp.setTemperature(temperature);
        produceGrowUp.setSoilPH(soilPH);
        produceGrowUp.setPesticideDose(pesticideDose);
        produceGrowUp.setOtherDescription(otherDescription);
        produceGrowUp.setGrowUp(growUp);
        produceGrowUp.setCollectionDate(DateUtil.getCollectionDate());

        return produceDao.addProduceGrowup(produceGrowUp);
    }

    @Override
    public List<ProduceGrowUp> getProduceGrowUp(Map params) {

        String uuid = (String) params.get("uuid");
        String growUp = (String) params.get("GrowUp");

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        List<ProduceGrowUp> list = produceDao.getProduceGrowUp(uuid,growUp);
        return list;
    }



    @Override
    public List<ProduceType> getProduceType() {
        return produceDao.getProduceType();
    }


}
